package com.app.bimo.module_accounts.mvp.persenter;


import com.alibaba.android.arouter.launcher.ARouter;
import com.app.bimo.commonbusiness.mvp.BasePresenter;
import com.app.bimo.commonbusiness.network.RetrofitServiceManager;
import com.app.bimo.commonbusiness.network.RxObservableUtil;
import com.app.bimo.commonbusiness.util.Constant;
import com.app.bimo.commonbusiness.util.LoginService;
import com.app.bimo.commonbusiness.util.RouterHub;
import com.app.bimo.library_db.helper.UserHelper;
import com.app.bimo.module_accounts.mvp.constract.PhoneLoginContract;

import androidx.collection.ArrayMap;

public class PhoneLoginPresenter extends BasePresenter<PhoneLoginContract.Model, PhoneLoginContract.View> {
    public PhoneLoginPresenter(PhoneLoginContract.Model model, PhoneLoginContract.View rootView) {
        super(model, rootView);
    }


    public void getCodeFromModel(String mobile, int type) {

        RxObservableUtil.subscribe(mModel.getCode(mobile, type), bindCustomLoadingLifecycle(true), bookStoreDataList -> {
            // 处理数据 直接获取到List<JavaBean> carBeans
            mRootView.getCodeDataNotify(bookStoreDataList);
        });
    }

    public void registerFromModel(ArrayMap<String,String> map) {
        RxObservableUtil.subscribe(mModel.phoneRegister(map), bindCustomLoadingLifecycle(true), bookStoreDataList -> {
            // 处理数据 直接获取到List<JavaBean> carBeans
//            getUserInfoFromModel();

            RetrofitServiceManager.getInstance().addAllHead(Constant.HttpToken, bookStoreDataList.getToken());
            ((LoginService) ARouter.getInstance().build(RouterHub.ACCOUNT_SERVICE).navigation()).getUserInfo();
            mRootView.registerDataNotify(bookStoreDataList);
        });
    }

    /**
     * 获取用户资料保存到db
     */
    public void getUserInfoFromModel() {
        RxObservableUtil.subscribe(mModel.getUserInfo(), bindCustomLifecycle(), bookStoreDataList -> {
            // 处理数据 直接获取到List<JavaBean> carBeans
            if (UserHelper.getsInstance().findUser() != null) {
                UserHelper.getsInstance().removeUser();
            }
            UserHelper.getsInstance().saveUser(bookStoreDataList);
            mRootView.userInfoNotify(bookStoreDataList);
        });
    }

}
